package com.taobaolite.order_has_items;

import com.taobaolite.item.Item;
import com.taobaolite.order.Order;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.io.Serializable;
import java.util.Objects;

@Embeddable
public class OrderHasItemsPK implements Serializable {
    @ManyToOne
    @JoinColumn(name = "orderId")
    private Order order;
    @ManyToOne
    @JoinColumn(name = "itemId")
    @Cascade(CascadeType.ALL)
    private Item item;

    public OrderHasItemsPK() {}
    public OrderHasItemsPK(Order order, Item item) {
        this.order = order;
        this.item = item;
    }

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof OrderHasItemsPK)) return false;
        OrderHasItemsPK that = (OrderHasItemsPK) o;
        return order.equals(that.order) && item.equals(that.item);
    }

    @Override
    public int hashCode() {
        return Objects.hash(order, item);
    }
}
